using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
[PlayerStateScript(EPlayerState.Throw)]
public class PlayerThrowState : PlayerStateBase
{
    private float throwSpeed;
    private const float throwSpeedIncreaseRatePerSec = 16;
    private float holdingTime;
    private const float MAXholdingTime = 0.6f;
    private Vector3 offSet = new Vector3(0, 0.5f, 0);
    public override void EnterState()
    {
        // Debug.Log("Thorw");
        holdingTime = 0;
        throwSpeed = 0;
    }

    public override void ExitState()
    {
    }

    public override void HandleInput(EPlayerInput playerInput, EInputType inputType)
    {
        switch (playerInput)
        {
            case EPlayerInput.Throw:
                if (inputType == EInputType.Holding)
                {
                    // Debug.Log(throwSpeed);
                    if (holdingTime < MAXholdingTime)
                    {
                        holdingTime += Time.deltaTime;
                        throwSpeed = holdingTime * throwSpeedIncreaseRatePerSec;
                        // Debug.Log(throwSpeed);
                    }
                    else
                    {
                        PlayerAttribute.Instance.ShootArrow(GetThrowVector() * throwSpeed,offSet);
                        stateMachine.SetState(EPlayerState.Idle);
                    }
                }
                if (inputType == EInputType.End)
                {
                    PlayerAttribute.Instance.ShootArrow(GetThrowVector() * throwSpeed,offSet);
                    stateMachine.SetState(EPlayerState.Idle);
                }
                break;
        }
    }

    private Vector3 GetThrowVector()
    {
        var screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        var worldPos = new Vector3(screenPos.x, screenPos.y, 0);
        var deltaVector = (worldPos - stateMachine.playerComponentContex.playerTransform.position).normalized;
        return deltaVector;
    }

    public PlayerThrowState(PlayerStateMachine sm) : base(sm)
    {
    }
}
